import React from 'react';

import BlogHeader from '../components/blog/BlogHeader';

import {LoadScript} from '../common/Common';

import Review from '../components/blog/Review';

import Fetch from '../common/Fetch';

import {Icon} from 'amazeui-react';

class Blog extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data:{},
            load:true
        };
        this.blog_id = this.props.params.id;
    }

    componentDidMount() {
        this.loadData();
    }

    loadData() {
        Fetch("/res/blog/get",{blog_id:this.blog_id},(res)=>{
            if (res.status) {
                this.setState({
                    data:res.data,
                    load:false
                });
                document.title = this.state.data.blog_title;
                this.loadReview();
            }
        });
    }

    loadReview() {
        Review.load();
    }

    render() {
        return (
            <div className="content_main">
                <BlogHeader/>
                <div className="am-container">
                    {this.state.load ? this.renderLoad():this.renderBlog()}
                </div>
            </div>
        );
    }

    renderLoad() {
        return (
            <div className="blog-loading">
                <Icon size="lg" spin fw icon="spinner"/>
                <span>正在加载</span>
            </div>
        );
    }

    renderBlog() {
        var item = this.state.data;
        return (
            <div className="blog-list">
                <div className="blog-title">{item.blog_title}</div>
                <div className="blog-content markdown-body"
                     dangerouslySetInnerHTML={{__html:item.blog_html || ''}}>
                </div>
                <div className="blog-foot">
                    {moment.unix(item.created_date).format('YYYY-MM-DD HH:mm:ss')}
                </div>
                <div id="SOHUCS" ></div>
            </div>
        );
    }
}

Blog.contextTypes = {
    router: React.PropTypes.object
};

export default Blog;